package aes.server;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import common.Course;

/**
 * This class represents manager used in order to manage courses
 *
 */
public class CoursesManager {

	/**
	 * Courses list getter from DB
	 * @return Course List
	 */
	public static Course[] getCoursesList(){
		ArrayList<Course> arr=new ArrayList<Course>();
		DBManager db = DBManager.getInstance();
		try {
			String sql ="select cId, cName From courses Order by cName" ;
			PreparedStatement stmt1 = db.getConnection().prepareStatement(sql);
			ResultSet rs1 = stmt1.executeQuery();
			while(rs1.next()){
				Course c = new Course();
				c.setId(rs1.getInt("cId"));
				c.setName(rs1.getString("cName"));
				arr.add(c);
			}
			rs1.close();
			Course[] ret = new Course[arr.size()];
			arr.toArray(ret);
			return ret;
		}
		catch(SQLException ex){
			return null;
		}
		
	}

}
